<?php

namespace Home\Controller;

/**
 * 系统管理
 *
 * @author Administrator
 *        
 */
class SystemController extends BaseController {
	/**
	 * 获取管理员列表
	 */
	public function mb_GetAdminList() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		if ($adminData ['level'] == 0) {
			// 总管理员
			$where = "level != 0";
		} else if ($adminData ['level'] == 1) {
			// 平台上分
			$this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
			return;
		} else if ($adminData ['level'] == 2) {
			// 一级代理
			$code = $adminData ['code'];
			$subCode = mb_substr ( $code, 0, 5 );
			if ($where != '')
				$where .= ' AND ';
			$where .= "code like '" . $subCode . "___' AND level = '3'";
		} else if ($adminData ['level'] == 3) {
			// 二级代理
			$this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
			return;
		}
		$adminDatas = $adminInfo->where ( $where )->select ();
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $adminDatas ) );
	}
	/**
	 * 更新管理员状态
	 */
	public function mb_UpdateAdminStatus() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$name = $data->name;
		$type = $data->type;
		$status = $data->status;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$userInfo = M ( 'Userinfo' );
		if ($type == 0) {
			// 封该帐号
			$adminInfo->where ( "name='$name'" )->save ( array (
					'status' => $status 
			) );
		} else {
			// 封旗下玩家
			$adminData = $adminInfo->where ( "name='$name'" )->find ();
			$level = $adminData ['level'];
			$code = $adminData ['code'];
			if ($level == 2) {
				$subCode = mb_substr ( $code, 0, 5 );
				$userInfo->where ( "code like '" . $subCode . "___'" )->save ( array (
						'status' => $status 
				) );
			} else if ($level == 3) {
				$userInfo->where ( "code = '$code'" )->save ( array (
						'status' => $status 
				) );
			}
		}
		$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
	}
	
	/**
	 * 新增代理
	 */
	public function mb_AddAdmin() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$code = $data->code;
		$nick = $data->nick;
		$name = $data->name;
		$basePass = $data->pass;
		$encPass = md5 ( $basePass . C ( 'MSG_MD5' ) );
		$phone = $data->phone;
		$level = $data->level;
		$profitMode = $data->profitmode;
		$profitRate = $data->profitrate;
		$betRate = $data->betrate;
		$period = $data->period;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		$adminCode = $adminData ['code'];
		
		if ($adminData ['sw_agent'] == 1) {
			$this->mb_OutMessage ( ErrCode_Toast, '当前禁止创建代理' );
			return;
		}
		
		$adminData = $adminInfo->where ( "name='$name'" )->find ();
		if ($adminData != null) {
			$this->mb_OutMessage ( ErrCode_Toast, '该帐号已存在！' );
			return;
		}
		
		if ($level == 1) {
			// 平台上分
			$adminInfo->add ( array (
					'name' => $name,
					'nick' => $nick,
					'basepass' => $basePass,
					'encpass' => $encPass,
					'level' => $level,
					'code' => 1,
					'phone' => $phone,
					'profitmode' => $profitMode,
					'profitrate' => $profitRate,
					'betrate' => $betRate,
					'period' => $period 
			
			) );
		} else if ($level == 2) {
			// 一级代理
			$subCode = mb_substr ( $code, 0, 5 );
			$adminData = $adminInfo->where ( "code like '$subCode%'" )->find ();
			if (isset ( $adminData )) {
				$this->mb_OutMessage ( ErrCode_Toast, '该邀请码已存在' );
				return;
			}
			// $adminData = $adminInfo->where ( "level= '2'" )->order ( 'id desc' )->find ();
			// if (isset ( $adminData ))
			// $code = (mb_substr ( $adminData ['code'], 0, 5 ) + 1) * 1000;
			// else
			// $code = 10000000;
			
			$adminInfo->add ( array (
					'name' => $name,
					'nick' => $nick,
					'basepass' => $basePass,
					'encpass' => $encPass,
					'level' => $level,
					'code' => $code,
					'phone' => $phone,
					'profitmode' => $profitMode,
					'profitrate' => $profitRate,
					'betrate' => $betRate,
					'period' => $period 
			
			) );
		} else if ($level == 3) {
			// 二级代理
			if (mb_substr ( $adminCode, 0, 5 ) != mb_substr ( $code, 0, 5 )) {
				$this->mb_OutMessage ( ErrCode_Toast, '邀请码前五位不正确' );
				return;
			}
			$adminData = $adminInfo->where ( "code = '$code'" )->find ();
			if (isset ( $adminData )) {
				$this->mb_OutMessage ( ErrCode_Toast, '该邀请码已存在' );
				return;
			}
			
// 			$code = mb_substr ( $adminCode, 0, 5 );
// 			$adminData = $adminInfo->where ( "code like '" . $code . "___'" )->order ( 'id desc' )->find ();
// 			if (isset ( $adminData ))
// 				$code = $adminData ['code'] + 1;
// 			else
// 				$code ++;
			
			$adminInfo->add ( array (
					'name' => $name,
					'nick' => $nick,
					'basepass' => $basePass,
					'encpass' => $encPass,
					'level' => $level,
					'code' => $code,
					'phone' => $phone,
					'profitmode' => $profitMode,
					'profitrate' => $profitRate,
					'betrate' => $betRate,
					'period' => $period,
					'sw_agent' => 1 
			) );
		}
		$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 * 更新管理员信息
	 */
	public function mb_ResetAdminInfo() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$nick = $data->nick;
		$name = $data->name;
		$basePass = $data->pass;
		$encPass = md5 ( $basePass . C ( 'MSG_MD5' ) );
		$phone = $data->phone;
		$level = $data->level;
		$profitMode = $data->profitmode;
		$profitRate = $data->profitrate;
		$betRate = $data->betrate;
		$period = $data->period;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		if ($profitMode != $adminData ['profitmode'] || $profitRate != $adminData ['profitrate'] || $betRate != $adminData ['betrate'] || $period != $adminData ['period']) {
			
			$agentData = $adminInfo->where ( "name='$name'" )->find ();
			
			$adminProfitLog = M ( 'Adminprofitlog' );
			$adminProfitLog->add ( array (
					'admincode' => $agentData ['code'],
					'adminname' => $agentData ['name'],
					'beforetype' => $agentData ['profitmode'],
					'beforeprofitrate' => $agentData ['profitrate'],
					'beforebetrate' => $agentData ['betrate'],
					'beforeperiod' => $agentData ['period'],
					'type' => $profitMode,
					'profitrate' => $profitRate,
					'betrate' => $betRate,
					'period' => $period,
					'handlername' => $adminData ['name'],
					'handlercode' => $adminData ['code'] 
			) );
		}
		
		$adminInfo->where ( "name='$name'" )->save ( array (
				'nick' => $nick,
				'basepass' => $basePass,
				'ecnpass' => $encPass,
				'phone' => $phone,
				'profitmode' => $profitMode,
				'profitrate' => $profitRate,
				'betrate' => $betRate,
				'period' => $period 
		) );
		$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 * 获取修改分成日志
	 */
	public function mb_GetResetProfitLogs() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$adminProfitLog = M ( 'Adminprofitlog' );
		if ($adminData ['level'] == 0) {
			$logData = $adminProfitLog->order ( 'id desc' )->select ();
		} else if ($adminData ['level'] == 1) {
			$this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
			return;
		} else if ($adminData ['level'] == 2) {
			$code = $adminData ['code'];
			$subCode = mb_substr ( $code, 0, 5 );
			$logData = $adminProfitLog->where ( "admincode like '" . $subCode . "___'" )->order ( 'id desc' )->select ();
		} else if ($adminData ['level'] == 3) {
			$this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
			return;
		}
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $logData ) );
	}
}